package v1

import (
	"ginTea/model"
	"ginTea/utils/errmsg"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 添加轮播图
func AddCarousel(c *gin.Context) {
	var code int
	var data model.Carousel
	_ = c.ShouldBindJSON(&data)
	code = model.CreateCarousel(&data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": "success",
	})
}

// 删除轮播图
func DeleteCarousel(c *gin.Context) {
	var code int
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteCarousel(id)
	if code == errmsg.ERROR_CAROUSEL_NOT_EXIST {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	}
}

// // 获取轮播图列表
func GetCarouselList(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	page, _ := strconv.Atoi(c.Query("page"))
	if pageSize == 0 {
		pageSize = 10
	}
	if page == 0 {
		page = 1
	}
	data := model.GetCarouselList(page, pageSize)
	c.JSON(http.StatusOK, data)
}

// // 编辑轮播图
func UpdateCarousel(c *gin.Context) {
	var code int
	var data model.Carousel
	id, _ := strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)
	code = model.UpdateCarousel(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}
